package com.sto.stoapp.activity.scanner;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.sto.stoapp.StoApplication;
import com.sto.stoapp.activity.R;
import com.sto.stoapp.activity.StoBillControlsActivity;
import com.sto.stoapp.activity.aviation.RouteListActivity;
import com.sto.stoapp.activity.sales.SalesSetSignNameActivity;
import com.sto.stoapp.activity.scanner.business.BusinessListActivity;
import com.sto.stoapp.activity.scanner.business.StoBillCodeListActivity;
import com.sto.stoapp.activity.scanner.business.StoTouchScreenLockSetActivity;
import com.sto.stoapp.adapter.InquiryMenuAdapter;
import com.sto.stoapp.dataprocess.parcelable.SelectTableParcel;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.db.constant.RegionInfoConstant;
import com.sto.stoapp.db.constant.StoOrderReasonConstant;
import com.sto.stoapp.db.constant.StoProblemConstant;
import com.sto.stoapp.db.constant.StoSalesConstant;
import com.sto.stoapp.db.constant.StoServerSmsConstant;
import com.sto.stoapp.db.constant.StoSiteCodeConstant;
import com.sto.stoapp.db.manager.CastManager;
import com.sto.stoapp.db.manager.SqlSelectTableManager;
import com.sto.stoapp.entity.InquiryEntity;
import com.sto.stoapp.file.SmzFile;
import com.sto.stoapp.http.BaseMessage;
import com.sto.stoapp.http.MessageType;
import com.sto.stoapp.listener.OnBackClickListener;
import com.sto.stoapp.listener.StoOnItemClickListener;
import com.sto.stoapp.log.ExceptionLog;
import com.sto.stoapp.manager.AccessControlManager;
import com.sto.stoapp.server.StoDownServer;
import com.sto.stoapp.util.Helper;
import com.sto.stoapp.util.UnifiedStaticType;
import com.sto.stoapp.widget.BaseDialog;
import com.sto.stoapp.widget.StoHomeActivity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@SuppressLint("SimpleDateFormat")
public class StoBusinessSetupActivity extends StoHomeActivity {

	private ListView datalist = null; // 定义ListView组件
	private List<InquiryEntity> list = new ArrayList<InquiryEntity>(); // 定义显示的内容包装
	private InquiryEntity inquiryEntity;
	private InquiryMenuAdapter menuAdapter;
	private String busiType = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.sto_inquirymenu_list);
		Bundle extras = getIntent().getExtras();
		busiType = extras.getString("busiType");
		this.setTitleBottom(R.string.business_settings, busiType);

		initWidget();
	}

	private void initWidget() {
		this.datalist = (ListView) super.findViewById(R.id.dest_list); // 取得组件
		datalist.setOnItemClickListener(new StoOnItemClickListener(this) {

			@Override
			public void KcOnItemClickListener(AdapterView<?> parent, View view,
					int position, long id) {
				setSelectList(list.get(position).getMenuName());
			}
		});

		displayListView();
	}

	private void displayListView() {
		List<String> listItem = getBusinessList();
		int maxItem = listItem.size();

		for (int x = 0; x < maxItem; x++) {
			inquiryEntity = new InquiryEntity();
			inquiryEntity.setMenuName(listItem.get(x));
			inquiryEntity.setImageId(R.drawable.next);
			this.list.add(inquiryEntity); // 保存了所有的数据行

		}
		menuAdapter = new InquiryMenuAdapter(this, list);
		datalist.setAdapter(menuAdapter);
	}

	private void setSelectList(String info) {
		// 网点列表
		if (info.equals(getString(R.string.site_full_list))) {
			try {
				SelectTableParcel tableParcel = new SelectTableParcel();
				tableParcel.setSelectSql(SqlSelectTableManager.SQL_NETSITE_ALL);
				tableParcel.setSelectField(new String[] {});

				tableParcel.setSelectCode(StoSiteCodeConstant.SITE_CODE_NUMBER);
				tableParcel.setSelectName(StoSiteCodeConstant.SITE_CODE_NAME);
				tableParcel.setProperty(getString(R.string.site_full_list));

				Intent intent = new Intent(StoBusinessSetupActivity.this,
						BusinessListActivity.class);
				intent.putExtra("businessListData", tableParcel);

				startActivity(intent);
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("setSelectList", e.getMessage());
			}
		}
		// 员工列表
		else if (info.equals(getString(R.string.sales_list))) {
			try {
				SelectTableParcel tableParcel = new SelectTableParcel();
				tableParcel.setSelectSql(SqlSelectTableManager.SQL_STAFF_ALL);
				tableParcel.setSelectField(new String[] {});

				tableParcel.setSelectCode(StoSalesConstant.SALES_NUMBER);
				tableParcel.setSelectName(StoSalesConstant.SALES_NAME);
				tableParcel.setProperty(getString(R.string.sales_list));

				Intent intent = new Intent(StoBusinessSetupActivity.this,
						BusinessListActivity.class);
				intent.putExtra("businessListData", tableParcel);

				startActivity(intent);
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("setSelectList", e.getMessage());
			}
		}
		// 问题件原因表
		else if (info.equals(getString(R.string.problem_list))) {
			try {
				SelectTableParcel tableParcel = new SelectTableParcel();
				tableParcel.setSelectSql(SqlSelectTableManager.SQL_PROBLEM_ALL);
				tableParcel.setSelectField(new String[] {});

				tableParcel.setSelectCode(StoProblemConstant.REASON_CODE);
				tableParcel.setSelectName(StoProblemConstant.REASON_NAME);
				tableParcel.setProperty(getString(R.string.problem_list));

				Intent intent = new Intent(StoBusinessSetupActivity.this,
						BusinessListActivity.class);
				intent.putExtra("businessListData", tableParcel);

				startActivity(intent);
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("setSelectList", e.getMessage());
			}
		}
		// 实名制网点更新
		else if(info.equals("实名制网点更新")){
			Helper.showToast(StoApplication.getAppInstance(), "开始下载更新");
			new SmzFile(StoApplication.getAppInstance()).downSmzZjSiteCodeFile(true);
		}
		// 快件在线查询
		else if (info.equals(getString(R.string.online_inquiry_list))) {
			if (AccessControlManager.getInstance(this).getExpressQuery()) {
				Intent intent = new Intent(StoBusinessSetupActivity.this,
						StoOnlineInquiryActivity.class);
				intent.putExtra("busiType", busiType);
				startActivity(intent);
			} else {
				Helper.showSoundToast(this, "对不起,你没有权限,请让管理员开通", false);
			}
		}
		// 自动上传时间
		else if (info.equals(getString(R.string.auto_upload_time_list))) {
			Intent intent = new Intent(this,
					StoAutoUploadTimerSetActivity.class);
			intent.putExtra("busiType", busiType);
			// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			// startActivity(intent);
			startActivity(intent);

			/*
			 * try { showSetTimeDialog(); } catch (Exception e) {
			 * e.printStackTrace(); ExceptionLog.writeLog("setSelectList",
			 * e.getMessage()); }
			 */
		}
		// 网点账户列表
		else if (info.equals(getString(R.string.netsite_account_list))) {
			Intent downIntent = new Intent(StoDownServer.UPDATE_ACTION);
			downIntent.putExtra(StoDownServer.SERVER_DOWN_STATUS,
					new int[] { MessageType.OFFLINE_LOGIN, });
			sendBroadcast(downIntent);
		}
		// 下一站网点资料
		else if (info.equals(getString(R.string.next_site_list))) {
			try {
				SelectTableParcel tableParcel = new SelectTableParcel();
				tableParcel
						.setSelectSql(SqlSelectTableManager.SQL_NEXT_NETSITE_ALL);
				tableParcel.setSelectField(new String[] {});

				tableParcel.setSelectCode(StoSiteCodeConstant.SITE_CODE_NUMBER);
				tableParcel.setSelectName(StoSiteCodeConstant.SITE_CODE_NAME);
				tableParcel.setProperty(getString(R.string.next_site_list));
				Intent intent = new Intent(StoBusinessSetupActivity.this,
						BusinessListActivity.class);
				intent.putExtra("businessListData", tableParcel);

				startActivity(intent);
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("setSelectList", e.getMessage());
			}
		}
		// 网点省份列表
		else if (info.equals(getString(R.string.province_list))) {
			try {
				SelectTableParcel tableParcel = new SelectTableParcel();
				tableParcel
						.setSelectSql(SqlSelectTableManager.SQL_REGION_INFO_ALL);
				tableParcel.setSelectField(new String[] {});

				tableParcel.setSelectCode(RegionInfoConstant.CODE);
				tableParcel.setSelectName(RegionInfoConstant.NAME);
				tableParcel.setProperty(getString(R.string.province_list));
				Intent intent = new Intent(StoBusinessSetupActivity.this,
						BusinessListActivity.class);

				intent.putExtra("businessListData", tableParcel);

				startActivity(intent);
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("setSelectList", e.getMessage());
			}
		}
		// 服务短信列表
		else if (info.equals(getString(R.string.service_SMS_list))) {

			try {
				SelectTableParcel tableParcel = new SelectTableParcel();
				tableParcel
						.setSelectSql(SqlSelectTableManager.SQL_SERVER_SMS_ALL);
				tableParcel.setSelectField(new String[] {});

				tableParcel
						.setSelectCode(StoServerSmsConstant.SERVER_SMS_SITECODE);
				tableParcel
						.setSelectName(StoServerSmsConstant.SERVER_SMS_ADDRESS);
				tableParcel.setProperty(getString(R.string.service_SMS_list));

				Intent intent = new Intent(StoBusinessSetupActivity.this,
						BusinessListActivity.class);
				intent.putExtra("businessListData", tableParcel);

				startActivity(intent);
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("setSelectList", e.getMessage());
			}
		}
		// 常用签收名(业务员)
		else if (info.equals(getString(R.string.common_sign_name))) {
			Intent intent = new Intent(StoBusinessSetupActivity.this,
					SalesSetSignNameActivity.class);
			startActivity(intent);
		}
		// 呼叫中心订单
		else if (info.equals(getString(R.string.call_order))) {
			if (StoFilePreference.getInstance(this).getOrderFunctionControl()) {
				Intent downIntent = new Intent(StoDownServer.UPDATE_ACTION);
				downIntent.putExtra(StoDownServer.SERVER_DOWN_STATUS,
						new int[] { MessageType.ORDER_DOWNLOAD_TYPE });
				sendBroadcast(downIntent);
			} else {
				Helper.showToast(this, getText(R.string.order_function_no_open)
						.toString());
			}
		}
		// 订单打回原因
		else if (info.equals(getString(R.string.order_back_reason))) {
			try {
				SelectTableParcel tableParcel = new SelectTableParcel();
				tableParcel
						.setSelectSql(SqlSelectTableManager.SQL_ORDER_REASON_ALL);
				tableParcel.setSelectField(new String[] {});

				tableParcel
						.setSelectCode(StoOrderReasonConstant.ORDER_SERIAL_NUMBER);
				tableParcel
						.setSelectName(StoOrderReasonConstant.RECORD_CONTENT);
				tableParcel.setProperty(getString(R.string.order_back_reason));

				Intent intent = new Intent(StoBusinessSetupActivity.this,
						BusinessListActivity.class);
				intent.putExtra("businessListData", tableParcel);

				startActivity(intent);
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("setSelectList", e.getMessage());
			}
		}
		// 触屏锁设置
		else if (info.equals(getString(R.string.touch_screen_set))) {
			Intent intent = new Intent(this,
					StoTouchScreenLockSetActivity.class);
			intent.putExtra("busiType", busiType);
			// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			// startActivity(intent);
			startActivity(intent);
		}
		/*
		 * 路由表显示三项
		 * 
		 * @修改人：mgf
		 */
		// 路由表
		else if (info.equals(getString(R.string.route_list))) {
			try {
				Intent intent = new Intent(StoBusinessSetupActivity.this,
						RouteListActivity.class);
				startActivity(intent);
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("setSelectList", e.getMessage());
			}
		}
		// 删除当天到包预报
		else if (info.equals(getString(R.string.delte_cast_by_date))) {
			final BaseDialog dialog = new BaseDialog(
					StoBusinessSetupActivity.this);
			dialog.setTitles(getString(R.string.is_delte_cast));
			dialog.show();
			dialog.getvMessage().setVisibility(View.GONE);
			dialog.getvPresentation().setVisibility(View.GONE);
			dialog.setOnBackClickListener(new OnBackClickListener() {

				@Override
				public void onBackClickListener(boolean res) {
					if (res) {
						SimpleDateFormat sDateFormat = new SimpleDateFormat(
								UnifiedStaticType.DATE_FORMAT);
						String date = sDateFormat.format(new java.util.Date());
						CastManager.getInstance(StoBusinessSetupActivity.this)
								.deleteByDate(date);
						StoFilePreference.getInstance(
								StoBusinessSetupActivity.this)
								.clearForeCastInfo();
						Helper.showToast(StoBusinessSetupActivity.this,
								getString(R.string.delte_cast_by_date));
					}
					dialog.dismiss();
				}
			});
		}
		// 面单发放
		else if (info.equals(getString(R.string.billcode_list))) {
			startActivity(new Intent(StoBusinessSetupActivity.this,
					StoBillCodeListActivity.class));

		} else if ("单号规则下载".equals(info)) {
			Intent downIntent = new Intent(StoDownServer.UPDATE_ACTION);
			downIntent.putExtra(StoDownServer.SERVER_DOWN_STATUS,
					new int[] { MessageType.SCAN_RULE_TABLE });
			sendBroadcast(downIntent);
		}else if(info.equals(getString(R.string.bill_controls_table))){
			//面单发放记录表
			Intent intent = new Intent(StoBusinessSetupActivity.this,
					StoBillControlsActivity.class);

			startActivity(intent);
		}
	}

	/**
	 * 网点扫描员业务设置信息
	 * 
	 * @return
	 */
	private List<String> getBusinessList() {
		List<String> busList = new ArrayList<String>();

		// 网点扫描员
		if (UnifiedStaticType.loginType.equals(BaseMessage.SALES_VERSION)) {

			busList.add(getString(R.string.common_sign_name));
			busList.add(getString(R.string.bill_controls_table));
			busList.add(getString(R.string.problem_list));
			//busList.add(getString(R.string.billcode_list));
			busList.add(getString(R.string.call_order));
			busList.add(getString(R.string.order_back_reason));
			busList.add(getString(R.string.online_inquiry_list));
			busList.add(getString(R.string.netsite_account_list));
			busList.add(getString(R.string.province_list));
			busList.add(getString(R.string.auto_upload_time_list));
			busList.add(getString(R.string.service_SMS_list));
			busList.add("单号规则下载");
			busList.add("实名制网点更新");
			// busList.add(getString(R.string.touch_screen_set));
		}
		// 网点业务员
		else if (UnifiedStaticType.loginType
				.equals(BaseMessage.SCANNER_VERSION)) {
			busList.add(getString(R.string.site_full_list));
			busList.add(getString(R.string.bill_controls_table));
			busList.add(getString(R.string.sales_list));
			busList.add(getString(R.string.problem_list));
			//busList.add(getString(R.string.billcode_list));
			busList.add(getString(R.string.online_inquiry_list));
			busList.add(getString(R.string.auto_upload_time_list));
			busList.add(getString(R.string.netsite_account_list));
			busList.add(getString(R.string.next_site_list));
			busList.add(getString(R.string.province_list));
			busList.add(getString(R.string.service_SMS_list));
			busList.add("单号规则下载");
			// busList.add(getString(R.string.touch_screen_set));
		}// 中转
		else if (UnifiedStaticType.loginType.equals(BaseMessage.CENTER_VERSION)) {
			busList.add(getString(R.string.site_full_list));
			busList.add(getString(R.string.bill_controls_table));
			busList.add(getString(R.string.next_site_list));
			busList.add(getString(R.string.sales_list));
			busList.add(getString(R.string.problem_list));
			busList.add(getString(R.string.online_inquiry_list));
			busList.add(getString(R.string.auto_upload_time_list));
			busList.add(getString(R.string.netsite_account_list));
			busList.add(getString(R.string.province_list));
			busList.add("单号规则下载");
			// busList.add(getString(R.string.touch_screen_set));
		}
		// 航空扫描员
		else if (UnifiedStaticType.loginType
				.equals(BaseMessage.AVIATION_A_VERSION)) {
			busList.add(getString(R.string.site_full_list));
			busList.add(getString(R.string.bill_controls_table));
			busList.add(getString(R.string.sales_list));
			busList.add(getString(R.string.next_site_list));
			busList.add(getString(R.string.province_list));
			busList.add(getString(R.string.problem_list));
			busList.add(getString(R.string.route_list));
			busList.add(getString(R.string.netsite_account_list));
			busList.add(getString(R.string.online_inquiry_list));
			busList.add(getString(R.string.auto_upload_time_list));
			busList.add("单号规则下载");
			// busList.add(getString(R.string.touch_screen_set));
		}
		// 航空提货员
		else if (UnifiedStaticType.loginType
				.equals(BaseMessage.AVIATION_T_VERSION)) {
			busList.add(getString(R.string.online_inquiry_list));
			busList.add(getString(R.string.bill_controls_table));
			busList.add(getString(R.string.auto_upload_time_list));
			busList.add(getString(R.string.delte_cast_by_date));
			busList.add("单号规则下载");
			// busList.add(getString(R.string.touch_screen_set));
		}

		return busList;
	}



	// 设置自动上传的时间
	// private void showSetTimeDialog() {
	// int time = -1;
	// time = StoFilePreference.getInstance(StoBusinessSetupActivity.this)
	// .getAutoUploadTime();
	//
	// final EditText edit = new EditText(StoBusinessSetupActivity.this);
	// edit.setHint(String.valueOf(time) + "(分)");
	// Dialog dialog = new AlertDialog.Builder(StoBusinessSetupActivity.this)
	// .setIcon(android.R.drawable.ic_dialog_info)
	// .setTitle(getString(R.string.auto_upload_time_list) + "(分)")
	// .setPositiveButton(getString(R.string.confirm),
	// new DialogInterface.OnClickListener() {
	//
	// @Override
	// public void onClick(DialogInterface dialog,
	// int which) {
	// if (isDigit(edit.getText().toString())) {
	// int time = Integer.valueOf(edit.getText()
	// .toString());
	// if (time >= 0) {
	// StoFilePreference.getInstance(
	// StoBusinessSetupActivity.this)
	// .setAutoUploadTime(time);
	// Helper.showToast(
	// StoBusinessSetupActivity.this,
	// getString(R.string.set_success));
	// Intent autpIntent = new Intent(
	// UnifiedStaticType.TIMING_ACTION);
	// autpIntent
	// .putExtra(
	// UnifiedStaticType.TIMING_TYPE,
	// UnifiedStaticType.AUTO_UPLOAD_TIMING);
	// sendBroadcast(autpIntent);
	// }
	// }
	// }
	// }).setNegativeButton(getString(R.string.cancel), null)
	// .setView(edit).create();
	// dialog.show();
	//
	// }
}
